package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerReturnDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerReturnServiceImpl implements CustomerReturnService {
    @Autowired
    private CustomerReturnDao customerReturnDao;

    @Override
    public void saveReturnList(CustomerReturnList customerReturnList, String customerReturnListGoodsStr) {
//创建Gson 将String转为对象
        Gson gson = new Gson();
        customerReturnDao.saveReturnList(customerReturnList);
        List<Map<String, Object>> list = gson.fromJson(customerReturnListGoodsStr, List.class);
        for (Map<String, Object> map : list) {
            CustomerReturnListGoods crlg = new CustomerReturnListGoods();
            crlg.setGoodsId(Integer.parseInt(map.get("goodsId").toString().split("\\.")[0]));
            crlg.setGoodsCode((String) map.get("goodsCode"));
            crlg.setGoodsName((String) map.get("goodsName"));
            crlg.setGoodsModel((String) map.get("goodsModel"));
            crlg.setGoodsUnit((String) map.get("goodsUnit"));
            crlg.setGoodsNum(Integer.parseInt((String) map.get("goodsNum")));
            Integer price = Integer.parseInt(map.get("price").toString().split("\\.")[0]);
            Integer total = Integer.parseInt(map.get("total").toString().split("\\.")[0]);
            crlg.setCustomerReturnListGoodsId(crlg.getCustomerReturnListId());
            crlg.setGoodsTypeId(Integer.parseInt(map.get("goodsTypeId").toString().split("\\.")[0]));
            customerReturnDao.saveReturnListGoods(crlg, price, total);
        }
    }

    @Override
    public Map<String, Object> getReturnList(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<ReturnList> lists = customerReturnDao.getReturnList(returnNumber, customerId, state, sTime, eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", lists);
        return map;
    }

    @Override
    public Map<Object, Object> getReturnListGoods(Integer customerReturnListId) {
        List<ReturnListGoods> returnListGoodsList = customerReturnDao.getReturnListGoods(customerReturnListId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rows", returnListGoodsList);
        return map;
    }

    @Override
    public void returnDelete(Integer customerReturnListId) {
        //删除退货单对应的商品信息
        customerReturnDao.returnDeleteGoods(customerReturnListId);
        //删除退货单
        customerReturnDao.returnDelete(customerReturnListId);
    }

    @Override
    public List<CustomerReturnList> getCusReturnCount(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<CustomerReturnList> getCusReList = customerReturnDao.getCusReturnCount(sTime, eTime, goodsTypeId, codeOrName);
        return getCusReList;
    }
}

